//
//  UserToken.m
//  TheDragon
//
//  Created by 杜文杰 on 2017/7/8.
//  Copyright © 2017年 LJ. All rights reserved.
//

#import "UserToken.h"

@implementation UserToken

// 存储token
- (void)saveToken:(NSString *)token
{
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSData *tokenData = [NSKeyedArchiver archivedDataWithRootObject:token];
    [userDefaults setValue:tokenData forKey:TOKEN_KEY];
    
    [userDefaults synchronize];
}

// 读取token
- (NSString *) getToken
{
//    UserToken *token1 = [[UserToken alloc] init];
//    [token1 saveToken:@"SXlNalltRXpNamt4WldZMU5Ea3pOamd6TXprek5tTTFOemt3TjJNM05HWXdZVGtKTVRVPVJ2a0c3QjJQ"];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *tokenData = [userDefaults objectForKey:TOKEN_KEY];
    NSString *token = [NSKeyedUnarchiver unarchiveObjectWithData:tokenData];
//    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    [userDefaults synchronize];
    return token;
}

// 清空token
- (void)cleanToken
{
    NSUserDefaults *UserLoginState = [NSUserDefaults standardUserDefaults];
    [UserLoginState removeObjectForKey:TOKEN_KEY];
    [UserLoginState synchronize];
}
@end
